package team.southcat.wsyygh.hospital.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.southcat.wsyygh.common.result.Result;
import team.southcat.wsyygh.common.util.AdminInfoUtil;
import team.southcat.wsyygh.hospital.service.ScheduleService;
import team.southcat.wsyygh.model.pojo.hospital.Schedule;
import team.southcat.wsyygh.model.pojo.hospital.ScheduleVo;
import team.southcat.wsyygh.model.pojo.user.Admin;
import team.southcat.wsyygh.user.client.AdminFeignClient;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author QiuSiYi
 * @since 2023-04-20
 */
@Api(tags = "医生排班管理接口")
@RestController
@RequestMapping("/admin/hospital/schedule")
public class ScheduleController {
    @Resource
    private ScheduleService scheduleService;

    @Autowired
    private AdminFeignClient adminFeignClient;

    @ApiOperation("分页查询医生排班列表")
    @GetMapping("/getSchedulePageList/{page}/{limit}")
    public Result getSchedulePageList(@PathVariable int page, @PathVariable int limit, ScheduleVo scheduleVo, HttpServletRequest request){
        String adminId = AdminInfoUtil.getAdminId(request);
        //系统管理端也访问，不带token来
        if (adminId != null){
            Object adminObj = adminFeignClient.getAdminById(Integer.valueOf(adminId)).getData();
            Admin admin = JSONObject.parseObject(JSON.toJSONString(adminObj), Admin.class);
            scheduleVo.setHospitalId(Long.valueOf(admin.getHospitalId()));
        }

        //map包括departments total
        Page pageResult = scheduleService.getSchedulePageList(page,limit,scheduleVo);
        return Result.ok(pageResult);
    }

    @ApiOperation("根据id删除医生排班信息")
    @DeleteMapping("/deleteScheduleById/{id}")
    public Result deleteScheduleById(@PathVariable int id){
        boolean flag = scheduleService.removeById(id);
        return flag ? Result.ok() : Result.fail().message("删除失败！");
    }

    @ApiOperation("批量删除医生排班信息")
    @DeleteMapping("/batchDeleteScheduleByIds")
    public Result batchDeleteScheduleByIds(@RequestBody List<Integer> ids){
        boolean flag = scheduleService.removeByIds(ids);
        return flag ? Result.ok() : Result.fail().message("删除失败！");
    }

    @ApiOperation("更改医生排班状态")
    @PutMapping("/changeSwitch/{id}")
    public Result changeSwitch(@PathVariable int id){
        Schedule schedule = scheduleService.getById(id);
        Integer status = schedule.getStatus();
        if(status == 1){
            status=0;
        }else if(status == 0){
            status=1;
        }
        schedule.setStatus(status);
        boolean flag = scheduleService.updateById(schedule);
        return flag ? Result.ok() : Result.fail().message("更新状态失败！");

    }

    @ApiOperation("添加医生排班信息")
    @PostMapping("/addSchedule")
    public Result addSchedule(@RequestBody ScheduleVo schedule, HttpServletRequest request){
        // todo 排班添加不了，实在解决不了改成单天添加好了
        String adminId = AdminInfoUtil.getAdminId(request);
        Result result = scheduleService.addSchedule(schedule,adminId);
        return result;
    }

    @ApiOperation("根据id查询医生排班信息")
    @GetMapping("/getScheduleById/{id}")
    public Result getScheduleById(@PathVariable Integer id){
        Result result =  scheduleService.getScheduleById(id);
        return result;
    }

    @ApiOperation("修改医生排班信息")
    @PostMapping("/updateSchedule")
    public Result updateSchedule(@RequestBody Schedule schedule){
        Result result = scheduleService.updateSchedule(schedule);
        return result;
    }

    @GetMapping("/getScheduleByDoctorIds")
    public Result getScheduleByDoctorIds(@RequestBody List<Long> doctorIds){
        List<Schedule> schedules = scheduleService.getScheduleByDoctorIds(doctorIds);
        return Result.ok(schedules);
    }
}

